<template>
    <el-row class="lives-news">
        <el-col class="title">
            <h3>文学作品</h3>
        </el-col>
        <el-col>
            <el-form :inline="true">
                <el-form-item>
                    <el-button type="primary" @click="showAddDialog"><i class="el-icon-plus el-icon--left"></i>添加文件</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <el-col>
            <el-table :data="list">
            	<el-table-column prop="title" label="作品名称" align="center"></el-table-column>
                <el-table-column label="作品连接" width="180px" >
                	<template slot-scope="scope">
                   		<a :href="scope.row.jumpUrl" target="_blank">{{scope.row.jumpUrl}}</a>
                	</template>
                </el-table-column>
                <el-table-column label="操作" width="160px" align="center">
                    <template slot-scope="scope">
                        <el-button size="small" @click="openUpdateObjDialog(scope.$index, scope.row)">修改</el-button>
                        <el-button size="small" type="danger" @click="deletObj(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-col>
        <el-dialog :title="isAdd?'新增作品':'修改作品'" :visible.sync="showDialog" size="tiny" @close="closeDialog" class="dialog">
            <el-form :model="data" label-width="120px">
                <el-form-item label="作品名称">
                    <el-input v-model="data.title"></el-input>
                </el-form-item>
                <el-form-item label="作品链接">
                    <el-input v-model="data.jumpUrl"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="save" :loading="addLoading">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 分页 -->
        <el-col class="pagination">
            <el-pagination @current-change="currentChange" :current-page="params.pageId" :page-size="params.pageSize" layout="total, prev, pager, next" :total="counts">
            </el-pagination>
        </el-col>
    </el-row>
</template>
<script>
import {
	literaryWorksList,
	literaryWorksAdd,
	literaryWorksUpdate,
	literaryWorksDelete
} from '@/api/api'
export default {
    data() {
        return {
        	list: [],
        	data: {
        		jumpUrl: '',
        		literaryWorksId: 0,
        		title: ''
        	},
        	params: {
                pageId: 1,
                pageSize: 10,
        	},
            counts: 0,
            isAdd: true,
            showDialog: false,
            addLoading: false
        }
    },
    created() {
        this.pageId = parseInt(this.$route.query.page) || 1;
        this.getList();
    },
    methods: {
        getList() {
        	literaryWorksList(this.params).then(data => {
                this.counts = data.count;
                this.list = data.list;
            })
        },
        //分页
        currentChange(val) {
            this.$router.push('?page=' + val)
            this.params.pageId = val;
            this.getList()
        },
        //显示添加弹窗
        showAddDialog() {
            this.showDialog = true;
        },
        //关闭添加弹窗
        closeDialog() {
            this.isAdd = true;
            this.addLoading = false;
            this.data = {
           		jumpUrl: '',
           		literaryWorksId: 0,
           		title: ''
           	}
        },
        openUpdateObjDialog(index, row) {
            this.isAdd = false;
            this.showDialog = true;
            this.data = row
        },
        save() {
            this.addLoading = true;
            if(!this.data.title){
                this.$message({
                    message: '请填写作品名称',
                    type: 'error'
                })
                return;
            }
            if(!this.data.jumpUrl){
                this.$message({
                    message: '请填写作品地址',
                    type: 'error'
                })
                return;
            }
            
            if (this.isAdd) {
            	literaryWorksAdd(this.data).then(data => {
                    this.getList();
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    })
                    this.showDialog = false;
                })
            } else {
            	literaryWorksUpdate(this.data).then(data => {
                    this.getList();
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    })
                    this.showDialog = false;
                }).catch(err => {
                    console.error(err)
                })
            }
        },
        deletObj(index, row) {
            this.$confirm('是否删除文学作品?', '删除文学作品', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
            	literaryWorksDelete(row.literaryWorksId).then(() => {
                    this.getList();
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
    }
}
</script>
<style scoped>
.lives-news {
    .title {
        font-size: 12px;
        border-bottom: 1px solid #eaeaea;
        margin-bottom: 5px;
        h3 {
            display: inline-block;
            padding: 5px 0;
            margin: 0;
            font-weight: normal;
            color: #23b7e5;
            border-bottom: 1px solid #23b7e5;
        }
    }
    .pictureURL{
        word-wrap: break-word;
    }
}
</style>